package com.gomore.opple.utils;

import android.graphics.Bitmap;

import com.lightbox.android.photoprocessing.PhotoProcessing;
import com.lightbox.android.photoprocessing.utils.BitmapUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by asus on 2017/11/17.
 */

public class PhotoUtils {

    public static void getCompressPhoto(String path, String value) {
        Bitmap bp = getUploadImageNative(path, UploadQuality.mapFromPreference(value));
        try {
            FileOutputStream fOut = new FileOutputStream(path);
            bp.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
            fOut.flush();
            fOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public enum UploadQuality {
        LOW(640, 75), MEDIUM(800, 80), HIGH(1080, 85), ORIGINAL(
                Integer.MAX_VALUE, 90);
        private final int mMaxDimension, mJpegQuality;

        UploadQuality(int maxDimension, int jpegQuality) {
            mMaxDimension = maxDimension;
            mJpegQuality = jpegQuality;
        }

        public int getMaxDimension() {
            return mMaxDimension;
        }

        public int getJpegQuality() {
            return mJpegQuality;
        }

        public boolean requiresResizing() {
            return mMaxDimension < Integer.MAX_VALUE;
        }

        public static UploadQuality mapFromPreference(String value) {
            UploadQuality returnValue = MEDIUM;

            if ("0".equals(value)) {
                returnValue = LOW;
            } else if ("1".equals(value)) {
                returnValue = MEDIUM;
            } else if ("2".equals(value)) {
                returnValue = HIGH;
            } else if ("3".equals(value)) {
                returnValue = ORIGINAL;
            }
            return returnValue;
        }
    }

    public static Bitmap getUploadImageNative(String path,
                                              final UploadQuality quality) {
        try {
            if (null != path) {
                BitmapUtils.BitmapSize size = BitmapUtils.getBitmapSize(path);

                if (quality.requiresResizing()) {
                    final float resizeRatio = Math.max(size.width, size.height) / (float) quality.getMaxDimension();
                    size = new BitmapUtils.BitmapSize(Math.round(size.width / resizeRatio), Math.round(size.height / resizeRatio));
                }
                File jpegFile = new File(path);
                byte[] jpegData = com.lightbox.android.photoprocessing.utils.FileUtils.readFileToByteArray(jpegFile);
                PhotoProcessing.nativeLoadResizedJpegBitmap(jpegData, jpegData.length, size.width * size.height);
                return PhotoProcessing.getBitmapFromNative(null);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            PhotoProcessing.nativeDeleteBitmap();
        }
        return null;
    }
}
